/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.browser.admin;

import ar.com.neolokura.travian.helper.browser.item.HTMLTag;
import ar.com.neolokura.travian.helper.constant.TravianWebTree;
import com.zenkey.net.prowser.Request;
import com.zenkey.net.prowser.Tab;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author gustavo
 */
public class ProwserHandler {
    
    public ProwserHandler() {
        
    }
    
    public Tab loginProwser(String userName, String password, Tab tab) throws URISyntaxException{

        TravianWebTree tree = new TravianWebTree();
        
        //Gets the page source and gets the html tags.
        tab.go(tree.getLoginPage());
        String html = tab.getPageSource();
        ArrayList<HTMLTag> htmlTags = getUserTags(html);

        String loginInfo = "";
        for (HTMLTag tag : htmlTags) {
            if (tag.getName().equalsIgnoreCase("login") || tag.getId().equals("login")) {
                loginInfo = tag.getValue();
            }
        }

        // Creates a new Request for the desired url
        Request request = new Request(tree.getHomePage());
        request.setHttpMethod("POST");
        request.addParameter("name", userName);
        request.addParameter("password", password);
        request.addParameter("lowRes", "1");
        request.addParameter("s1", "Login");
        request.addParameter("login", loginInfo);
        request.addParameter("w", "1600%3A900");

        tab.go(request);
        

        return tab;
    }
    
     /**
     * Gets a list of tags for the user from a raw html to be interpreted by someone. The incoming html should be the login page.
     * 
     * @param html Raw html of the login page for Travian 4
     * 
     * @return ArrayList<HTMLTag> A list of user tags needed for login
     * 
     * @see HTMLTag
     * 
     * @since JDK1.6
     * @since 2011-07-09
     * 
     */
    private ArrayList<HTMLTag> getUserTags(String html) {

        ArrayList<HTMLTag> tags = new ArrayList<HTMLTag>();

        HTMLTag name = new HTMLTag("", "name", "text", "text", "");
        HTMLTag password = new HTMLTag("", "password", "password", "text", "");
        HTMLTag lowRes = new HTMLTag("lowRes", "lowRes", "checkbox", "", "1");
        HTMLTag s1 = new HTMLTag("s1", "s1", "submit", "", "Login");
        HTMLTag w = new HTMLTag("", "w", "hidden", "", "1252:704");

        Pattern pattern = Pattern.compile("(?<=<input type=\"hidden\" name=\"login\" value=\")[0-9]*(?=\" />)");
        Matcher matcher = pattern.matcher(html);

        matcher.find();
        HTMLTag login = new HTMLTag("login", "login", "hidden", "", matcher.group());

        tags.add(name);
        tags.add(password);
        tags.add(lowRes);
        tags.add(s1);
        tags.add(w);
        tags.add(login);

        return tags;
    }
    
}
